<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      <el-form-item label="菜单名称：">
        <el-input v-model="query.name" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'menu:search'" icon='el-icon-search' type="primary" @click="fetchData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
        <el-button v-permission="'menu:add'" icon="el-icon-circle-plus-outline" type="primary" @click="handleAdd(0)">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%"
      row-key="id">
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="name"
        label="名称"
        width="140">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="url"
        label="请求地址"
        width="140">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="code"
        label="权限标识"
        width="140">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="type"
        label="类型"
        width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.type===1">目录</span>
          <span v-if="scope.row.type===2">菜单</span>
          <span v-if="scope.row.type===3">按钮</span>
        </template>
      </el-table-column>
      <el-table-column 
        align="center"
        prop="icon"
        label="图标"
        width="80">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="sort"
        label="排序"
        width="80">
      </el-table-column>
      <el-table-column  
        align="center"   
        label="操作"
        min-width="210">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            v-permission="'menu:add'"
            @click="handleAdd(scope.row.id)">新增</el-button>
          <el-button
            size="mini"
            type="success"
            v-permission="'menu:edit'"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-permission="'menu:delete'"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出窗口 -->
    <edit :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose" :title="edit.title" ></edit>
  </div>
</template>

<script>
import api from '@/api/menu'
import Edit from './edit.vue'

export default {
  name:'Menu',
  components: { Edit },
  data() {
    return {
      list:[],
      query:{},// 查询条件
      edit:{
        visible:false,
        formData:{
        },
        title:''
      }
    }
  },
  methods: {
    // 请求数据
    fetchData(){
      api.getList(this.query).then(
        responce=>{
          if(responce.code===20000){
            this.list=responce.data
          }else{
            this.$message.error('请求数据失败');
          }
          
        }
      )
    },
    // 编辑
    handleEdit(id){
      api.getById(id).then((response)=>{
          
          if (response.code === 20000){
            this.edit.formData=response.data
            this.edit.title = '编辑'
            this.edit.visible = true
            
          }
        })

    },
    // 增加
    handleAdd(id){
      this.edit.formData.parentId=id
      this.edit.visible=true
      this.edit.title='新增'
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
     // 重载
    reload(){
    this.query={}
    this.fetchData()
    },
    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.edit.visible=false
      this.edit.formData={}
      this.fetchData()

    },
  },
  created() {
    this.fetchData()
  },
}
</script>

<style>

</style>